package com.lark.web.controller;

import java.io.IOException;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
import com.lark.common.annotation.Log;
import com.lark.common.core.domain.R;
import com.github.pagehelper.PageInfo;
import com.lark.common.core.page.PageDomain;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.lark.common.enums.BusinessType;
import com.lark.common.utils.poi.ExcelUtil;
import com.lark.framework.cache.DictUtils;
import javax.validation.constraints.*;

import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import com.lark.common.core.controller.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import com.lark.web.domain.Image;
import com.lark.web.service.ImageService;
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;

/**
 * 镜像Controller
 * @author fei
 * @email 952597665@qq.com
 * @date 2024-08-11
 */
@RestController
@RequestMapping("/dockerinsight/image")
public class ImageController extends BaseController {

    @Autowired
    private ImageService imageService;

    /**
     * 查询镜像列表
     */
    @PreAuthorize("@ss.hasPermi('dockerinsight:image:list')")
    @GetMapping("/list")
    public R<PageInfo> list(Image image, HttpServletRequest request, HttpServletResponse response) {
        image.setPage(new PageDomain(request, response));

        return R.data(imageService.findPage(image));
    }

    /**
     * 获取镜像详细信息
     */
    @PreAuthorize("@ss.hasPermi('dockerinsight:image:query')")
    @GetMapping(value = "/{id}")
    public R<Image> detail(@PathVariable("id") String id) {
        return R.data(imageService.get(id));
    }

    /**
     * 新增镜像
     */
    @PreAuthorize("@ss.hasPermi('dockerinsight:image:add')")
    @Log(title = "镜像", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated  Image image) {
        return R.status(imageService.save(image));
    }

    /**
     * 修改镜像
     */
    @PreAuthorize("@ss.hasPermi('dockerinsight:image:edit')")
    @Log(title = "镜像", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated Image image) {
        return R.status(imageService.save(image));
    }

    /**
     * 删除镜像
     */
    @PreAuthorize("@ss.hasPermi('dockerinsight:image:remove')")
    @Log(title = "镜像", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids) {
        return R.status(imageService.deleteImageByIds(ids));
    }

    @PreAuthorize("@ss.hasPermi('dockerinsight:image:start')")
    @Log(title = "镜像", businessType = BusinessType.OTHER)
    @PostMapping("/startImage")
    public R startImage(@RequestBody Image image) {
        image.setImageTag(image.getImageRepository()+":"+image.getImageTag());
        return R.status(imageService.startImage(image));
    }

    @PreAuthorize("@ss.hasPermi('dockerinsight:image:remove')")
    @Log(title = "镜像", businessType = BusinessType.OTHER)
    @PostMapping("/removeImage")
    public R removeImage(@RequestBody Image image) {
        return R.status(imageService.remove(image));
    }


    /**
     * 导出镜像列表
     */
    @PreAuthorize("@ss.hasPermi('dockerinsight:image:export')")
    @Log(title = "镜像", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(Image image) {
        List<Image> list = imageService.findList(image);
        ExcelUtil<Image> util = new ExcelUtil<Image>(Image.class);
        return util.exportExcel(list, "镜像数据");
    }

    @PreAuthorize("@ss.hasPermi('dockerinsight:image:export')")
    @Log(title = "镜像", businessType = BusinessType.EXPORT)
    @GetMapping("/exportImage")
    public ResponseEntity<StreamingResponseBody> exportImage(@RequestParam String tag, @RequestParam String ip, HttpServletResponse response) throws IOException {
        Image image=new Image();
        image.setIp(ip);
        image.setImageTag(tag);

        return imageService.exportImage(image,response);
    }



    /**
     * 根据字典类型查询字典数据信息等其他自定义信息
     */
    @GetMapping(value = "/getInitData/{dictTypes}")
    public R getInitData(@PathVariable String dictTypes) {
        Map<String, Object> dataMap = new HashMap<String, Object>();
        dataMap.putAll(DictUtils.getMultiDictList(dictTypes));
        return R.data(dataMap);
    }

}
